/**
 * Initialisation.java
 * 
 * Stores the values that the user set to initalise the world. Is also responsible
 * for generating all of the static data (houses, regions, appliances) for the world.
 */
package com.swin.energy;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;

public class Initialisation {

	public int minNumberOfHouses = 1;
	public int maxNumberOfHouses;
	public int numberOfAppliances;
	public int maxNumberOfInhabitants = 6;
	public int nosOfRegions;

	public int totalNumberOfAppliances;
	public int totalNumberOfHouses;

	public String name;

	// These names correspond to the profiles stored in appliances.txt
	public String[] appliances = { "fridge", "tv", "dryer", "heater", "pc" };

	/**
	 * String format contains all the information used to generate data for
	 * appliance usage
	 */
	public List<String> initialiseArea() {
		List<String> area;
		totalNumberOfAppliances = 0;
		totalNumberOfHouses = 0;
		area = new ArrayList<String>();
		for (int region = 0; region < nosOfRegions; region++) {
			String regionName = regionName(region);

			int nosOfHouses = numberOfHouses();
			totalNumberOfHouses += nosOfHouses;
			for (int house = 0; house < nosOfHouses; house++) {
				String houseName = houseName(house);

				int nosOfAppliances = numberOfAppliances();
				totalNumberOfAppliances += nosOfAppliances;
				for (int appliance = 0; appliance < nosOfAppliances; appliance++) {
					String applianceName = applianceName(appliance);
					boolean hasInsulation = trueOrFalse();
					boolean isEnvFriendly = trueOrFalse();
					
					// TODO: Extend system to handle extra influences
					area.add(regionName + "," + houseName + "," + applianceName
							+ "," + generateNumberOfInhabitants() + ","
							+ hasInsulation + "," + isEnvFriendly);
				}
			}
		}
		return area;
	}

	/**
	 * Must be called after initialiseArea()
	 * 
	 * @return
	 */
	public int getTotalNumberOfHouses() {
		return totalNumberOfHouses;
	}

	private String regionName(Integer value) {
		return "Region" + value;
	}

	// TODO: Radomise and have actual names
	private String houseName(Integer value) {
		return "House" + value;
	}

	private String applianceName(Integer value) {
		Random rand = new Random();
		return appliances[rand.nextInt(appliances.length)];
	}

	public int numberOfHouses() {
		// TODO: Fix bug with number of houses and neural network
		// Random rand = new Random();
		// return rand.nextInt(maxNumberOfHouses - minNumberOfHouses + 1)
		// + minNumberOfHouses;
		return maxNumberOfHouses;
	}

	public int numberOfAppliances() {
		// TODO: work out suitable number of appliances
		// Random rand = new Random();
		// return rand.nextInt(numberOfAppliances - 1) + 1;
		return 1;
	}

	public int generateNumberOfInhabitants() {
		Random rand = new Random();
		return rand.nextInt(maxNumberOfInhabitants - 1) + 1;
	}

	private boolean trueOrFalse() {
		Random rand = new Random();
		return rand.nextBoolean();
	}

	public void setNosOfRegions(int nosOfRegions) {
		this.nosOfRegions = nosOfRegions;
	}

	public int getNosOfRegions() {
		return this.nosOfRegions;
	}

	public void setMaxNumberOfHouses(int maxNumberOfHouses) {
		this.maxNumberOfHouses = maxNumberOfHouses;
	}

	public void setNumberOfAppliances(int numberOfAppliances) {
		this.numberOfAppliances = numberOfAppliances;
	}

	public int getMaxNumberOfHouses() {
		return this.maxNumberOfHouses;
	}

	public int getNumberOfAppliances() {
		return this.numberOfAppliances;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getName() {
		return this.name;
	}

	/**
	 * Must be called after initialiseArea
	 * 
	 * @return total number of appliances 
	 */
	public int getTotalNumberOfAppliances() {
		return totalNumberOfAppliances;
	}

}
